package org.smart4j.framework.helper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 封装ServletAPI 让controller 与serverAPI 解耦
 * 保证是线程暗安全的，每一个请求对应一个ServletHelper实例 请求结束销毁
 */
public final class ServletHelper {


    /**
     * 每个线程独自拥有一个ServletHelper实例
     */
    private static  final ThreadLocal<ServletHelper> POOL = new ThreadLocal<>();

    private HttpServletRequest request;
    private HttpServletResponse response;

    public ServletHelper(HttpServletRequest request,HttpServletResponse response){
        this.request = request;
        this.response = response;
    }

    /**
     * 初始化POOL
     * @param request
     * @param response
     */
    public static void init(HttpServletRequest request,HttpServletResponse response){

        POOL.set(new ServletHelper(request,response));
    }

    public HttpServletRequest getRequest() {
        return POOL.get().request;
    }

    public HttpServletResponse getResponse() {
        return POOL.get().response;
    }

    public static void destory(){
        POOL.remove();
    }
}
